package pe.com.cotelasa.common.servicios;

import java.util.Map;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.exception.VelocityException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Component;
import org.springframework.ui.velocity.VelocityEngineUtils;

@Component("emailMessagePreparator")
@Scope("singleton")
public class EmailMessagePreparator implements MimeMessagePreparator, InitializingBean{

	@Autowired
	private VelocityEngine velocityEngine;

	private String plainTextTemplate = "plainText.vm";

	private String htmlTemplate = "html.vm";

	private String[] from;

	private String[] to;

	private String subject;

	private Map data;

	private String[] cc;

	private String bseLogoFilePath;

	private String bseStylesheetPath;

	private String[] attachFile;
	
	public void afterPropertiesSet() throws Exception {

	}

	private void readFromAddresses(MimeMessage mimeMessage) {
		try {
			if (from == null || from.length == 0) {
				return;
			}
			for (int i = 0; i < from.length; i++) {
				mimeMessage.addFrom(InternetAddress.parse(from[i]));
			}
		}
		catch (MessagingException msgExc) {
			msgExc.printStackTrace();
		}
	}

	private void readToAddresses(MimeMessage mimeMessage) {
		try {
			if (to == null || to.length == 0) {
				return;
			}
			for (int i = 0; i < to.length; i++) {
				mimeMessage.addRecipients(Message.RecipientType.TO, InternetAddress.parse(to[i]));
			}
		}
		catch (MessagingException msgExc) {
			msgExc.printStackTrace();
		}
	}

	private void readCcAddresses(MimeMessage mimeMessage) {
		try {
			if (cc == null || cc.length == 0) {
				return;
			}
			for (int i = 0; i < cc.length; i++) {
				mimeMessage.addRecipients(Message.RecipientType.CC, InternetAddress.parse(cc[i]));
			}
		}
		catch (MessagingException msgExc) {
			msgExc.printStackTrace();
		}
	}

	public void prepare(MimeMessage mimeMessage) throws MessagingException, VelocityException {

		String fileName = null;
		
		int indexFileName = 0;
		
		this.readFromAddresses(mimeMessage);

		this.readToAddresses(mimeMessage);

		this.readCcAddresses(mimeMessage);
		// try {
		mimeMessage.setSubject(this.subject);

		// create wrapper multipart/alternative part
		MimeMultipart ma = new MimeMultipart("alternative");
		mimeMessage.setContent(ma);

		// create the plain text
//		BodyPart plainText = new MimeBodyPart();
//		plainText.setText(VelocityEngineUtils
//				.mergeTemplateIntoString(velocityEngine, this.plainTextTemplate, this.data));
//		ma.addBodyPart(plainText);

		// create the html and image multipart wrapper
		BodyPart related = new MimeBodyPart();
		MimeMultipart mr = new MimeMultipart("related");
		related.setContent(mr);
		ma.addBodyPart(related);

		// create the html part
		BodyPart html = new MimeBodyPart();
//		html.setContent(VelocityEngineUtils.mergeTemplateIntoString(velocityEngine,null, this.data),"text/html");
		html.setContent("HOLLLLA","text/html");
		mr.addBodyPart(html);

//		if (attachFile != null) {
//
//			for (int i = 0; i < attachFile.length; i++) {
//				BodyPart attach = new MimeBodyPart();
//				// attach.setHeader("Message-ID", "0001");
//				attach.setDisposition("ATTACHMENT");
//				attach.setDataHandler(new DataHandler(new FileDataSource(this.attachFile[i])));
//				fileName = this.attachFile[i];
//				indexFileName = fileName.lastIndexOf('/');  
//				fileName = fileName.substring(indexFileName + 1);
//				//attach.setFileName(this.attachFile[i]);
//				attach.setFileName(fileName);
//				mr.addBodyPart(attach);
//
//			}
//		}

		/*
		 * //add attachment content BodyPart img = new MimeBodyPart();
		 * //img.setHeader("Content-ID","BlueStarLogo");
		 * img.setDisposition("attachment"); URL urlLogoLocation =
		 * ClassUtils.getResource(this.bseLogoFilePath, getClass());
		 * //img.setDataHandler(new DataHandler(new
		 * FileDataSource(this.bseLogoFilePath))); img.setDataHandler(new
		 * DataHandler(urlLogoLocation)); mr.addBodyPart(img);
		 * 
		 * //add stylesheet BodyPart styleSheet = new MimeBodyPart();
		 * styleSheet.setHeader("Content-ID","BlueStarStyleSheetSrc");
		 * styleSheet.setDisposition("inline"); URL urlStyleLocation =
		 * ClassUtils.getResource(this.bseStylesheetPath, getClass());
		 * //styleSheet.setDataHandler(new DataHandler(new
		 * FileDataSource(this.bseStylesheetPath)));
		 * styleSheet.setDataHandler(new DataHandler(urlStyleLocation));
		 * mr.addBodyPart(styleSheet);
		 */
		// }
		/*
		 * catch(MessagingException msgExc) { msgExc.printStackTrace(); throw
		 * msgExc; } catch(VelocityException velExc) { velExc.printStackTrace();
		 * throw velExc; }
		 */

	}

	public String getBseLogoFilePath() {
		return bseLogoFilePath;
	}

	public void setBseLogoFilePath(String bseLogoFilePath) {
		this.bseLogoFilePath = bseLogoFilePath;
	}

	public String getBseStylesheetPath() {
		return bseStylesheetPath;
	}

	public void setBseStylesheetPath(String bseStylesheetPath) {
		this.bseStylesheetPath = bseStylesheetPath;
	}

	public String[] getCc() {
		return cc;
	}

	public void setCc(String[] cc) {
		this.cc = cc;
	}

	public Map getData() {
		return data;
	}

	public void setData(Map data) {
		this.data = data;
	}

	public String[] getFrom() {
		return from;
	}

	public void setFrom(String[] from) {
		this.from = from;
	}

	public String getHtmlTemplate() {
		return htmlTemplate;
	}

	public void setHtmlTemplate(String htmlTemplate) {
		this.htmlTemplate = htmlTemplate;
	}

	public String getPlainTextTemplate() {
		return plainTextTemplate;
	}

	public void setPlainTextTemplate(String plainTextTemplate) {
		this.plainTextTemplate = plainTextTemplate;
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public String[] getTo() {
		return to;
	}

	public void setTo(String[] to) {
		this.to = to;
	}

	public VelocityEngine getVelocityEngine() {
		return velocityEngine;
	}

	public void setVelocityEngine(VelocityEngine velocityEngine) {
		this.velocityEngine = velocityEngine;
	}

	public String[] getAttachFile() {
		return attachFile;
	}

	public void setAttachFile(String[] attachFile) {
		this.attachFile = attachFile;
	}


}
